package main.com.oaer.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import main.com.oaer.model.UserRole;
import main.com.oaer.service.UserRoleService;
import main.com.oaer.util.CommonUtil;

/**
 * 用户、角色控制器
 *
 */
@Controller
@RequestMapping("/userRole")
public class UserRoleAction {
	
	@Autowired
	UserRoleService userRoleService;
	
    /**
     * 为用户分配一个角色，以json格式异步返回消息
     * @param response  回应对象
     * @param userId  用户id
     * @param roleId  角色id
     */
	@RequestMapping(value="/add")
	public void add(HttpServletResponse response,Long userId, Long roleId){
		UserRole userRole = new UserRole();
		userRole.setRoleId(roleId);
		userRole.setUserId(userId);
		userRoleService.add(userRole);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", "success");
		CommonUtil.printToHTML(response, map);
	}
	
	/**
	 * 查找用户的所属角色，并转到分配角色页面
	 * @param modleMap  模型对象
	 * @param userId  用户id
	 * @return  "system/assignRole"  分配角色页面
	 */
	@RequestMapping(value="/assignRole")
	public String assignRole(ModelMap modleMap, Long userId){
		List<Map<String, Object>> userRoleList = userRoleService.findAllRoles(userId);
		modleMap.put("userRoleList",userRoleList);
		modleMap.put("userId",userId);
		return "system/assignRole";
	}
}
